<?

include_once("private.inc.php");

class Role {
  private $id;
  private $name;
  private $user;
  private $event;
  
  function getID() {
    return $this->id;
  }
  
  function setID($id) {
    $this->id = $id;
  }
  
  function getName() {
    return $this->name;
  }
  
  function setName($n) {
    $this->name = $n;
  }
  
  function getUser() {
    return $this->user;
  }
  
  function getUserID() {
    $user = $this->getUser();
    if (empty($user)) return 0;
    return $user->getID();
  }
  
  function setUser($user_id) {
    if (empty($user_id)) {
      $this->user = NULL;
      return;
    }
    
    $query = "SELECT * FROM `".USER_TABLE."` WHERE `id`=".$user_id.";";
    $result = mysql_query($query) or die(mysql_error());
    if ($line = mysql_fetch_assoc($result)) {
      $user = new User();
      $user->setAttributes($line);
      $this->user = $user;
    }
    else {
      $this->user = NULL;
    }
  }
  
  function getEvent() {
    return $this->event;
  }
  
  function getEventID() {
    $event = $this->getEvent();
    if (empty($event)) return 0; // note: something is wrong if this happens! maybe it should just crash?
    return $event->getID();
  }
  
  function setEvent($event_id) {
    if (empty($event_id)) {
      $this->event = NULL;
      return;
    }
    
    $query = "SELECT * FROM `".EVENT_TABLE."` WHERE `id`=".$event_id.";";
    $result = mysql_query($query) or die(mysql_error());
    if ($line = mysql_fetch_assoc($result)) {
      $event = new Event();
      $event->setAttributes($line);
      $this->event = $event;
    }
    else {
      $this->event = NULL;
    }
  }
  
  function setAttributes($array) {
    $this->setID($array['id']);
    $this->setName($array['name']);
    $this->setUser($array['person_id']);
    $this->setEvent($array['activity_id']);
  }
}

?>
